package cn.elwy.rcp.ui.explorer.create;

import java.io.File;

import cn.elwy.rcp.core.model.ui.Condition;
import cn.elwy.rcp.core.model.ui.InputModel;
import cn.elwy.rcp.ui.explorer.core.AppContext;
import cn.elwy.rcp.ui.explorer.core.model.Operation;
import cn.elwy.rcp.ui.explorer.core.service.ProjectConfigService;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;


public class NewProjectModel extends InputModel {

  public static final String OPERATION = "operation";
  public static final String FILE_PATH = "filePath";

  private ProjectConfigService service = AppContext.getService(ProjectConfigService.ID, ProjectConfigService.class);

  public NewProjectModel(Condition condition) {
    super();
    setCondition(condition);
  }

  protected Object query() {
    Condition condition = getCondition();
    Operation operation = condition.get(NewProjectModel.OPERATION);
    File file = condition.get(NewProjectModel.FILE_PATH);
    switch (operation) {
    case NEW:
    case COPY:
      ProjectConfig newPorject = service.getNewPorject();
      newPorject.setProjectName("");
      newPorject.setVersion("");
      newPorject.setProjectPath("");
      return newPorject;
    case EDITOR:
      if (file != null && file.exists()) {
        return service.load(file);
      } else {
        return service.load();
      }
    case READ:
      return null;
    }
    return null;
  }

}
